package ch10.content;

import ch10.Date.UserDao;
import ch10.UserBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String userCode = request.getParameter("userCode");
    String userPassword = request.getParameter("userPassword");
        UserDao dao = new UserDao();
        UserBean user = null;
        try {
            user = dao.findByUserCode(userCode,userPassword);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (user == null){
                request.setAttribute("message","用户名或者密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }else {
                request.setAttribute("user",user);
                request.getRequestDispatcher("/success.jsp").forward(request,response);
            }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
